<template>
<uni-shadow-root class="partials-near_mendain_list-index"><block v-if="(!receiveData.androidTemplate||receiveData.androidTemplate=='typeOne')">
  <scroll-view scroll-x="true" class="near_mendain_list" v-if="nearMendainList&&nearMendainList.length!=0">
      <block v-for="(item,index) in (nearMendainList)" :key="item.id" :data-link="'mendian_detail.html?mendianId='+(item.id)" @click.native="tolinkUrl">
          <view class="near_mendain_item" v-if="limitState?limitState>index:'true'">
              <view class="near_mendain_banner">
                  <image :src="item.banner" mode="aspectFill" class="near_mendain_banner_img"></image>
              </view>
              <view class="near_mendain_info">
                  <text class="near_mendain_name">{{item.name}}</text>
                  <text class="near_mendain_distance">{{item.distance}}km</text>
              </view>
          </view>
      </block>
  </scroll-view>
  <view class="noproduct" v-else>
    <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/16/15/54/55_950.jpg?x-oss-process=style/preview_120"></image>
    <text>附近暂无门店~</text>
  </view>
</block>
<block v-if="receiveData.androidTemplate == 'typeTwo'">
   
</block></uni-shadow-root>
</template>

<script>

global['__wxRoute'] = 'partials/near_mendain_list/index'
const app = getApp();
Component({
  properties: {
    receiveData: {
      type: Object,
      value: 'default value',
    },
  },
  data: {
    setting: null, 
    nearMendainList: null,
    limitState:0,
    params:null,
    menDian:null,
    listPage:{
        page: 1,
        pageSize: 20,
        totalSize: 0,
    },
  },
  ready: function () {
    let that = this;
    console.log("====nearMendainList-data=====", that.data.receiveData);
    that.initSetting();
    if (that.data.receiveData.jsonData ) {
        that.data.params=Object.assign({},that.data.params,that.data.receiveData.jsonData)
        that.getData(that.data.params)
        if(that.data.receiveData.jsonData.count){
            that.setData({ limitState: that.data.receiveData.jsonData.count })
        }
    }
    console.log("====nearMendainList-limitState=====", that.data.limitState)
    console.log("====nearMendainList-params=====", that.data.params)
  },
  methods: {
    setDataFun:function(data){
        let that=this;
        console.log("setDataFun",data)
        that.setData({params: data})
        that.getData(data)
    },
    /* 获取数据 */
    getData: function (param) {
      let that = this
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          let latitude1 = res.latitude;
          let longitude1 = res.longitude;
          console.log(longitude1 + "..............." + latitude1);
          let menDian = {
            longitude: longitude1,
            latitude: latitude1
          };
          that.setData({
            menDian: menDian
          }); // longitude 经度 
          let params=Object.assign({},param,menDian)
          let customIndex = app.globalData.AddClientUrl("/find_mendians.html", params)
          app.globalData.showToastLoading('loading', true)
          wx.request({
            url: customIndex.url,
            header: app.globalData.header,
            success: function (res) {
              wx.hideLoading()
              console.log("==find_mendiansdata===",res.data)
              let data=res.data.relateObj.result
              if(data.length!=0){
                  for (let i = 0; i < data.length; i++) {
                    if (data[i].latitude && data[i].latitude != 0 && data[i].longitude && data[i].longitude != 0) {
                      data[i].distance = app.globalData.getDistance(menDian.latitude, menDian.longitude, data[i].latitude, data[i].longitude).toFixed(1);
                    }
                  }
              }
              that.data.listPage.pageSize = res.data.relateObj.pageSize
              that.data.listPage.page = res.data.relateObj.curPage
              that.data.listPage.totalSize = res.data.relateObj.totalSize
              let dataArr = that.data.nearMendainList;
              console.log("===find_mendiansdata====",data)
              if (param.page == 1) {
                dataArr = []
              }
              if (!data || data.length == 0) {
                that.setData({ nearMendainList: [] })
              } else {
                if (dataArr == null) { dataArr = [] }
                dataArr = dataArr.concat(data)
                that.setData({ nearMendainList: dataArr })
              }
              wx.hideLoading()
            },
            fail: function (res) {
              console.log("fail")
              wx.hideLoading()
              app.globalData.loadFail()
            }
          })
        },
        fail: function (err) {
          console.log(err);
          let menDian = {
            longitude: 0,
            latitude: 0
          }; // longitude 经度        
          // 获取门店的样式
          wx.showToast({
            title: "位置定位失败~",
            image: '/wxcomponents/images/icons/tip.png',
            duration: 1000
          });
        }
      });
    },
    calling: function (e) {
      console.log('====e===', e)
      let phoneNumber = e.currentTarget.dataset.phonenumber
      wx.makePhoneCall({
        phoneNumber: phoneNumber, //此号码并非真实电话号码，仅用于测试
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
    toIndex() {
      app.globalData.toIndex()
    },
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget.dataset.link
      app.globalData.linkEvent(linkUrl)
    },
    initSetting() {
      this.setData({ setting: app.globalData.setting })
    },
    onPullDownRefresh: function () {
      this.data.params.name = ""
      this.data.listPage.page = 1
      this.data.params.page = 1
      this.getData(this.data.params)

    },
    onReachBottom: function () {
      var that = this
      if (that.data.listPage.totalSize > that.data.listPage.curPage * that.data.listPage.pageSize) {
        that.data.listPage.page++
        that.data.params.page++
        that.getData(that.data.params);
      }else{
          wx.showToast({
            title: "到底了~",
            image: '/wxcomponents/images/icons/tip.png',
            duration: 1000
          });
      }
    },
  }
})
export default global['__wxComponents']['partials/near_mendain_list/index']
</script>
<style platform="mp-weixin">
@import 'type_one/index.css';
</style>